//
//  UrlParameterFactory.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-17.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "UrlParameterFactory.h"
#import "CONSTS.h"
#import "CoreDataManager.h"
#import "LFAccount.h"
#import "JSON.h"


NSString *getV(void)
{
    NSString *dd = @"*";
    
    return dd;
}

NSString * getT(void)
{
    NSDate *date = [NSDate date];
    NSTimeInterval time1 = [date timeIntervalSince1970];
   // NSTimeInterval time2 = 1000.0 * 60.0 * 60.0 * 24.0 * 365.0 * 1970.0;
    //NSTimeInterval time3 = time1 + time2;
    
    ITTDINFO(@"%d", (int)time1);
    
    return [NSString stringWithFormat: @"%d", (int)time1];
}


NSString *getImei(void)
{
    UIDevice *device = [UIDevice currentDevice];
    NSString *udid = [device uniqueIdentifier];
    udid = [udid stringByReplacingOccurrencesOfString: @"-" withString: @""];
    //NSLog(@"%@", udid);
    NSString *imei = [udid substringToIndex: 15];
    
    return imei;
}

NSString *getImsi(void)
{
    UIDevice *device = [UIDevice currentDevice];
    NSString *udid = [device uniqueIdentifier];
    udid = [udid stringByReplacingOccurrencesOfString: @"-" withString: @""];
    
    NSString *imsi = [udid substringFromIndex: 18];
    
    return imsi;
}

NSString *getLoginData(void)
{
    NSString *data = [NSString stringWithFormat: @"{\"appkey\":\"%@\",\"callbackUrl\":\"%@\"}", appkey, CALL_BACK_URL];
    
    return data;
}

NSString *getLoginSign(void)
{
    //    sign未登录公式：
    //    md5(appSecret+api+v+imei+imsi+md5(data)+t)如果涉及到编码请使用gbk编码进行加
    
    NSString *v = getV();
    NSString *imei = getImei();
    NSString *imsi = getImsi();
    
    NSString *data = getLoginData();
    NSString *md5ForData = [data md5];
    
    NSString *t = getT();
    NSString *sign = [[NSString stringWithFormat: @"%@%@%@%@%@%@%@", appSecret, com_taobao_wireless_mtop_getLoginUrl, v, imei, imsi, md5ForData, t] md5];
    
    return sign;
}

NSString *getSign(NSDictionary *parameterDic)
{
    NSArray *array = nil;
	
	array = [parameterDic allKeys];
	array = [array sortedArrayUsingSelector:@selector(compare:)];
    NSMutableString *signStr = [NSMutableString string];
    
    for (NSString *key in array)
    {
        NSString *value = [parameterDic objectForKey: key];
        NSString *str = [NSString stringWithFormat: @"%@%@", key, value];
        [signStr appendString: str];
    }
    
    signStr = [NSString stringWithFormat: @"%@%@%@", appSecret, signStr, appSecret];
    ITTDINFO(@"%@", signStr);
    
    
    NSString *sign = [[signStr md5] uppercaseString];
    
    return sign;
}

NSString *getToken(void)
{
    CoreDataManager *coreDataManager = [CoreDataManager sharedCoreDataManager];
    LFAccount *account = [coreDataManager getCurrentLoggedInAccount];
    NSString *token = account.token;
    
    return token;
}

NSString *getNick(void)
{
    CoreDataManager *coreDataManager = [CoreDataManager sharedCoreDataManager];
    LFAccount *account = [coreDataManager getCurrentLoggedInAccount];
    NSString *nick= account.nick;
    
    return nick;
}

NSString *getDataForAutoLogin(void)
{
    /*
     http://api.m.taobao.com/rest/api2.do?
     api=com.taobao.client.sys.autologin&
     v=*&
     imei=35 7646.00.112178.2&
     imsi=8986011030452016267j&
     appkey=12241231&
     
     data={"token":" 7660735181ca52e98da27d63acf4484914091eb42e46e4d92e44ca82fc58b09ff70946f8c 58b5eb846d433bb8f219756a374cee495d64a5f28bf33d969fe5ef29d8d89c27e500c9c68 a8de5b9ce55848c67cd48e021245d43ef33c3a677a7c2feee9d028ac908f2874ef7181ed8 49c8b1409abd8706afc16fe09d72440b68b02","appkey":"12241231","toptoken":"6107dd aa023407edaa30eeb0a20f2140"}&
     
     t=1314718835&
     sign=fc7b9745235960d8d1c1dd358 37c1f9b&
     ttid=400000_12241231@yssdk_android_1.0.0
     */
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
    NSString *token = getToken();
    NSString *toptoken = getToptokenForAutoLogin();
    
    [dataDic setObject: token forKey: @"token"];
    [dataDic setObject: appkey forKey: @"appkey"];
    [dataDic setObject: toptoken forKey: @"toptoken"];
    
    NSString *dataStr = [dataDic JSONRepresentation];
    ITTDINFO(@"%@", dataStr);
    
    return dataStr;
}

NSString *getToptokenForAutoLogin(void)
{
    
    //topToken = md5(appKey+md5(appSecret)+nick+t),所有字段都是GBK
    NSString *appKey = appkey;
    NSString *nick = getNick();
    NSString *t = getT();
    
    ITTDINFO(@"appKey = %@\n nick = %@\n t = %@", appKey, nick, t);
    
    //topToken = md5(appKey+md5(appSecret)+nick+t),所有字段都是GBK
    NSString *topToken = [[NSString stringWithFormat: @"%@%@%@%@",  appKey, [appSecret md5], nick, t] md5];
    
    return topToken;
}
